#include "stdlib.h"
#include "string.h"
#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/ADC.h"
#include "Headers/GPIO.h"
/*
* this function is for set up the ATD module and add some line to configure the acelerometer
* parameters
*          none
* return nothing
* NOTE this function check the check sum received and make a new one if this two doesn't match at all the pointer is cleaned
*/
void setup_ATD(void){
  ATDC_ATDPU=1;
  ATDC_DJM=1;
  ATDC_RES8=0;
  ATDC_SGN=0;
  ATDC_PRS0=0;
  ATDC_PRS1=0;
  ATDC_PRS2=0;
  ATDC_PRS3=0;
  
  ATD1SC_ATDIE=0;
  ATD1SC_ATDCO=0;	//0 single convertion
  
  GPIO_config('B',cinco,output,on);
  PTBD_PTBD5 = 1;
  
}
/*
* this function is for read the ATD module just one single port
* parameters
*          no_adc-------> number of ATD module to be read
*          dato---------> pointer to the value read from the ATD module port
* return 1 if was some error and 0 for a well done read
* NOTE --
*/
char read_single_adc(char no_adc, word *dato){
	unsigned int adc1=0, trying = 0;
	if(no_adc <= 7){
	  if(no_adc == 0) ATD1PE = bit0;
  	else if(no_adc == 1) ATD1PE = bit1;
  	else if(no_adc == 2) ATD1PE = bit2;
  	else if(no_adc == 3) ATD1PE = bit3;
  	else if(no_adc == 4) ATD1PE = bit4;
  	else if(no_adc == 5) ATD1PE = bit5;
  	else if(no_adc == 6) ATD1PE = bit6;
  	else if(no_adc == 7) ATD1PE = bit7;
  	ATD1SC = no_adc;
	}else {
	  ATD1PE = bit0;
	  ATD1SC = 0;
	}
	while(!ATD1SC_CCF && trying < 1000){
	  __RESET_WATCHDOG();
	  trying++;
	}
	if(trying > 999)return 1;
	*dato = ATD1R; 
  return 0;
}